import { createSlice } from "@reduxjs/toolkit";

export const listSlice = createSlice({
  name: "todolist",
  initialState: {
    list: [
      {
        id: 1,
        name: "tom",
      },
      {
        id: 2,
        name: "jerry",
      },
    ],
  },
  reducers: {
    add: (state, action) => {
      state.list.push(action.payload);
    },
    remove: (state, action) => {
      state.list = state.list.filter((item) => item.id !== action.payload);
    },
  },
});

export const { add, remove } = listSlice.actions;

export const asyncRemove = (id) => (dispatch) => {
  setTimeout(() => {
    dispatch(remove(id));
  }, 1000);
};

export default listSlice.reducer;
